//Event Study-Reduced Form Models
eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\ES\"
	local plotSaveDir = "Plots\ES\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/ES/"
	local plotSaveDir = "Plots/ES/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local spec2Controls = "`spec1Controls'"
local spec3Controls = "`spec2Controls' i.vmtDummyExtSpec3"
local spec4Controls = "`spec2Controls' i.vmtDummyExtSpec4"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Event Study Table format
local fmtEventStudy = `"cells(b(fmt(3) star) se(par fmt(3))) keep(*boundRelYear*) nocon label compress longtable mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}") starlevels(* 0.1 ** 0.05 *** 0.01) addnotes("* p \textless 0.1, ** p \textless 0.05, *** p \textless 0.01" "Note: All models include state and year fixed effects as well as state-level time trends." "All models cluster standard errors by state.")"'


//Loop through the number of dummies +- one year prior to the event
foreach bound of num 6 11{
	
	preserve
	
	//Add Dummy Variables for Event Study
	//Define Bounds
	local lowerBound = -`bound'
	local upperBound = `bound'
	
	//Define the relative time period that we will use
	local relYear = -1

	//Determine the dummy index of the relative year
	local relIndex = 1-`lowerBound'+`relYear'
	
	//Generate New Variable
	gen boundRelYear = yearrelativeend
	//Enforce Bounds
	replace boundRelYear = `lowerBound' if boundRelYear<=`lowerBound'
	replace boundRelYear = `upperBound' if boundRelYear>=`upperBound'
	replace boundRelYear = . if yearrelativeend==.
	
	//Create shifted Bounds
	gen shiftedBoundRelYear = boundRelYear - `lowerBound'+1
	replace shiftedBoundRelYear = `relIndex' if boundRelYear==.



	//Create Dummies
	qui tab boundRelYear, gen(boundRelYear)
	//Figure out which index for the dummy variables is the last one
	local finalInd = `upperBound'-`lowerBound'+1


	// Drop the relative year column for ease of use in the event study.
	qui drop boundRelYear`relIndex'


	local missingDataChar = uchar(8224)
//	
// 	if `upperBound'==6{
// 		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total VMT" "relative to {&tau}=-1")"'
//
//		
// 		local plotTitle = "Event Study Relative Year Fixed Effects"
// 		local plotSubtitle = "(±`upperBound' Years)"
// 		local plotNote = "-Point estimates and 95% confidence intervals shown"
//		
// 		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
// 		label values shiftedBoundRelYear shiftedBoundRelYear
// 		local plotNote2 = ""
// 	}
// 	else{
// 		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total VMT" "relative to {&tau}=-1")"'
//		
//		
// 		local plotTitle = ""
// 		local plotSubtitle = "(±`upperBound' Years)"
// 		local plotNote = "Point estimates and 95% confidence intervals shown"
//		
// 		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
// 		label values shiftedBoundRelYear shiftedBoundRelYear
// 		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
// 	}
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1")"'
		

		
		local plotTitle = "Event Study Relative Year Fixed Effects"
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "-Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = ""
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear11 boundRelYear12 boundRelYear13"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res . boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10 boundRelYear11_res boundRelYear12_res"
		mat at = [-5, -4, -3, -2, 0, 1, 2, 3, 4, 5, 10]
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear13_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1") xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))"' //omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))
		gen tempx = 5
		gen tempy = 0
		local max_ind = 13
	}
	else{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1")"'
		
		
		local plotTitle = ""
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res boundRelYear6_res boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10_res . boundRelYear12_res boundRelYear13_res boundRelYear14_res boundRelYear15_res boundRelYear16_res boundRelYear17_res boundRelYear18_res boundRelYear19_res boundRelYear20_res boundRelYear21_res boundRelYear22_res"
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear6 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear12 boundRelYear13 boundRelYear14 boundRelYear15 boundRelYear16 boundRelYear17 boundRelYear18 boundRelYear19 boundRelYear20 boundRelYear21 boundRelYear22 boundRelYear23"
		
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear23_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1") xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0))"'
		
		gen tempx = 10 
		gen tempy = 0
		local max_ind = 23
	}

	//Preferred Models
	//Spec. 1
// 	qui reghdfe logvmt ib`relIndex'.shiftedBoundRelYear `spec1Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("rf_spec1`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logvmt, `es_controls', `spec1Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec1`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec1`upperBound'.png", name("rf_spec1`upperBound'") replace width(`widthPix')
	eststo spec1
	
	//Spec. 2
// 	qui reghdfe logvmt ib`relIndex'.shiftedBoundRelYear `spec2Controls', vce (cluster `clusterVar')  absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec2`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logvmt, `es_controls', `spec2Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec2`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec2`upperBound'.png", name("rf_spec2`upperBound'") replace width(`widthPix')
	eststo spec2
	
	//Spec. 3
// 	qui reghdfe logvmt ib`relIndex'.shiftedBoundRelYear `spec3Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec3`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logvmt, `es_controls', `spec3Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec3`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec3`upperBound'.png", name("rf_spec3`upperBound'") replace width(`widthPix') 
	eststo spec3
	//Spec. 4
// 	qui reghdfe logvmt ib`relIndex'.shiftedBoundRelYear `spec4Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("rf_spec4`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logvmt, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec4`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec4`upperBound'.png", name("rf_spec4`upperBound'") replace width(`widthPix')
	eststo spec4
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "VMT Event Study Results Small Window \label{tab:ESVMT`upperBound'}"
	}
	else{
		local title = "VMT Event Study Results Large Window \label{tab:ESVMT`upperBound'}"
	}
	
	graph combine rf_spec1`bound' rf_spec2`bound' rf_spec3`bound' rf_spec4`bound', ycommon
	graph export "`plotSaveDir'VMTRFESCombined`upperBound'.png", replace width(`widthPix')

	esttab spec1 spec2 spec3 spec4 using "`tableSaveDir'VMTRFES`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	
	//Per-Capita Models
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear13_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1") xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))"'

	}
	else{
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear23_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1") xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0))"'
		
		
	}
	
	local spec3Controls = "`spec2Controls' i.vmtPerCapitaDummyExtSpec3"
	local spec4Controls = "`spec2Controls' i.vmtPerCapitaDummyExtSpec4"
	
	//Spec. 1
// 	qui reghdfe logVMTPerCapita ib`relIndex'.shiftedBoundRelYear `spec1Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec1PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logVMTPerCapita, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec1PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec1PerCapita`upperBound'.png", name("rf_spec1PerCapita`upperBound'") replace width(`widthPix') 
	eststo spec1PerCapita
	
	//Spec. 2
// 	qui reghdfe logVMTPerCapita ib`relIndex'.shiftedBoundRelYear `spec2Controls', vce (cluster `clusterVar')  absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec2PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logVMTPerCapita, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec2PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec2PerCapita`upperBound'.png", name("rf_spec2PerCapita`upperBound'") replace width(`widthPix') 
	eststo spec2PerCapita
	
	//Spec. 3
// 	qui reghdfe logVMTPerCapita ib`relIndex'.shiftedBoundRelYear `spec3Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec3PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logVMTPerCapita, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec3PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec3PerCapita`upperBound'.png", name("rf_spec3PerCapita`upperBound'") replace width(`widthPix') 
	eststo spec3PerCapita
	//Spec. 4
// 	qui reghdfe logVMTPerCapita ib`relIndex'.shiftedBoundRelYear `spec4Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("rf_spec4PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logVMTPerCapita, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("rf_spec4PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'VMTRFESSpec4PerCapita`upperBound'.png", name("rf_spec4PerCapita`upperBound'") replace width(`widthPix') 
	eststo spec4PerCapita
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "Travel Per-Capita Event Study Results Small Window \label{tab:ESPerCapita`upperBound'}"
	}
	else{
		local title = "Travel Per-Capita Event Study Results Large Window \label{tab:ESPerCapita`upperBound'}"
	}
	
	graph combine rf_spec1PerCapita`bound' rf_spec2PerCapita`bound' rf_spec3PerCapita`bound' rf_spec4PerCapita`bound', ycommon
	graph export "`plotSaveDir'VMTRFESCombinedPerCapita`upperBound'.png", replace width(`widthPix')

	esttab spec1PerCapita spec2PerCapita spec3PerCapita spec4PerCapita using "`tableSaveDir'VMTRFESPerCapita`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	restore
}
